import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  legalList: {},
  currentLegal: {
    content: "",
    image: "",
    provider: "",
    title: "",
    opt: "",
    video: "",
    cover: "",
    status: 3
  },
  searchLegalCondition: {
    pageSize: 10,
    title: "",
    status: ""
  }
};

const getters = {
  legalList: state => state.legalList,
  currentLegal: state => state.currentLegal,
  searchLegalCondition: state => state.searchLegalCondition
};

const actions = {
  getLegalPageS({ commit }, param) {
    return Api.getLegalPage(param).then(ret => {
      commit(types.UPDATE_LEGAL_LIST, ret.data.data);
      return ret;
    });
  },
  getLegalS({ commit }, id) {
    return Api.getLegal({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_LEGAL, ret.data.data);
      return ret;
    });
  },
  addOrUpdateLegalS({ commit }, payload) {
    return Api.addOrUpdateLegal({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteLegalS({ commit }, param) {
    return Api.deleteLegal(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_LEGAL](state, payload) {
    state.currentLegal = {
      content: "",
      image: "",
      provider: "",
      title: "",
      opt: "",
      video: "",
      cover: "",
      status: 3
    };
  },
  [types.UPDATE_CURRENT_LEGAL](state, payload) {
    state.currentLegal = payload;
  },
  [types.UPDATE_LEGAL_LIST](state, payload) {
    state.legalList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
